# Copyright (c) 2020 maminjie <maminjie1@huawei.com>
# SPDX-License-Identifier: MulanPSL-2.0

from __future__ import print_function

from utils import cmdln

from core import *

class Ican(cmdln.Cmdln):
    """Usage: ican [GLOBALOPTS] SUBCOMMAND [OPTS] [ARGS...]
    or: ican help SUBCOMMAND

    I-can-do-it command-line tool.
    Type 'ican help <subcommand>' for help on a specific subcommand.

    ${command_list}
    ${help_list}
    global ${option_list}
    """

    name = 'ican'
    conf = None

    def __init__(self, *args, **kwargs):
        cmdln.Cmdln.__init__(self, *args, **kwargs)
        cmdln.Cmdln.do_help.aliases.append('h')

    def get_version(self):
        return get_ican_version()

    def get_optparser(self):
        """this is the parser for "global" options (not specific to subcommand)"""

        optparser = cmdln.CmdlnOptionParser(self, version=get_ican_version())
        return optparser

    def get_cmd_help(self, cmdname):
        doc = self._get_cmd_handler(cmdname).__doc__
        doc = self._help_reindent(doc)
        doc = self._help_preprocess(doc, cmdname)
        doc = doc.rstrip() + '\n' # trim down trailing space
        return self._str(doc)
    
    def get_list(self, arg):
        pkgs = []
        if os.path.isfile(arg):
            with open(arg, 'r') as f:
                for ln in f:
                    pkgs.extend(ln.strip().split(" "))
        else:
            pkgs = list(arg.strip().split(" "))
        return pkgs

    @cmdln.alias('hello')
    def do_helloworld(self, subcmd, opts, *args):
        """${cmd_name}: Hello world

        usage:
            ${name}

        ${cmd_option_list}
        """
        print("hello world")
